<script setup lang="ts">
import { onMounted, ref } from "vue";
import { formRules } from "./utils/rule";
import type { FormProps } from "./utils/types";
import ReCol from "@/components/ReCol";
import { usePublicHooks } from "../hooks";
import { getRoleList, getRoleShow } from "@/api/roleApi";
const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    name: "",
    permissionChar: "",
    sort: 1,
    status: 2,
    remarks: "",
    menuIds: []
  })
});

const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const menuOptions = [
  {
    value: 1,
    label: "首页信息"
  },
  {
    value: 2,
    label: "部门管理"
  },
  {
    value: 3,
    label: "角色管理"
  },
  {
    value: 4,
    label: "任务管理"
  },
  {
    value: 5,
    label: "用户管理"
  }
];
function getRef() {
  return ruleFormRef.value;
}
onMounted(async () => {
  if (newFormInline.value.title != "新增") {
    getRoleShow({ rbacRoleId: newFormInline.value.rbacRoleId }).then(result => {
      newFormInline.value.menuIds = result.result.menuIds;
    });
  }
});
defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入角色名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="角色权限" prop="permissionChar">
          <el-input
            v-model="newFormInline.permissionChar"
            clearable
            placeholder="请输入角色权限"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="排序">
          <el-input-number
            v-model="newFormInline.sort"
            class="!w-full"
            controls-position="right"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="菜单权限">
          <el-select
            v-model="newFormInline.menuIds"
            placeholder="请选择菜单权限"
            class="w-full"
            clearable
            :multiple="true"
          >
            <el-option
              v-for="(item, index) in menuOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="角色状态">
          <el-switch
            v-model="newFormInline.status"
            inline-prompt
            :active-value="1"
            :inactive-value="2"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
            size="large"
          />
        </el-form-item>
      </re-col>
      <re-col :value="24" :xs="24" :sm="24">
        <el-form-item label="备注">
          <el-input
            v-model="newFormInline.remarks"
            placeholder="请输入备注信息"
            type="textarea"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
